package com.ebelter.myehc.ui.fragments.trends;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebelter.btcomlib.bases.fragment.BaseFragment;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.models.https.responses.BaseResponse;

import com.ebelter.myehc.R;
import com.ebelter.myehc.adapters.BraFragmentAdapter;
import com.ebelter.myehc.models.http.response.device_resports.BRA_DuanLian_R;
import com.ebelter.myehc.models.http.response.device_resports.BRA_SHM_R;
import com.ebelter.myehc.models.http.response.device_resports.BRA_XL_R;
import com.ebelter.myehc.ui.activitys.TrendActivity;


import org.greenrobot.eventbus.EventBus;


import java.util.ArrayList;
import java.util.List;

import static com.ebelter.btcomlib.bases.activity.BaseActivity.getString_;

/**
 * 锻炼趋势
 */

public class Trend_Bra_Fragment extends BaseFragment {
    public static final String TAG = "Trend_Bra_Fragment";




    private TextView top_title_tv;

    private RelativeLayout rl_dots;//底部滑动小圆点
    private LinearLayout in_ll;
    private ImageView mLight_dots;

    private ViewPager mViewPager;

    private BraFragmentAdapter mBraFragmentAdapter;

    Trend_Bra_duanlian_Fragment mTrend_Bra_duanlian_Fragment;//锻炼趋势 步数 里程 卡路里
    Trend_Bra_xinlv_Fragment mTrend_Bra_xinlv_Fragment;//心率
    Trend_Bra_shuimian_Fragment mTrend_Bra_shuimian_Fragment;//睡眠


    public void setTop_title_tv(TextView top_title_tv) {
        this.top_title_tv = top_title_tv;
    }

    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_trend_bra;
    }


    @Override
    protected void initView() {
        super.initView();
        FV();
        addDots();
        getDistance();
    }

    private void getDistance() {
        rl_dots.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //获得两个圆点之间的距离
                mDistance = in_ll.getChildAt(1).getLeft() - in_ll.getChildAt(0).getLeft();
                rl_dots.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                setLightPoint(0,0);
            }
        });
    }

    private void setLightPoint(int position, float positionOffset){
        float leftMargin = mDistance * (position + positionOffset);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mLight_dots.getLayoutParams();
        params.leftMargin = (int) leftMargin + mLeftRightMaginDistance;
        mLight_dots.setLayoutParams(params);
    }

    private float mDistance = 20;
    private int mLeftRightMaginDistance = 20;

    private void addDots() {
        ImageView mOne_dot = new ImageView(getContext());
        mOne_dot.setImageResource(R.drawable.circle1_background_shape);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(mLeftRightMaginDistance, 0, mLeftRightMaginDistance, 0);

        in_ll.addView(mOne_dot, layoutParams);

        ImageView mTwo_dot = new ImageView(getContext());
        mTwo_dot.setImageResource(R.drawable.circle1_background_shape);
        in_ll.addView(mTwo_dot, layoutParams);

        ImageView mThree_dot = new ImageView(getContext());
        mThree_dot.setImageResource(R.drawable.circle1_background_shape);
        in_ll.addView(mThree_dot, layoutParams);
    }

    private void FV() {
        rl_dots = mRootView.findViewById(R.id.rl_dots);
        in_ll = mRootView.findViewById(R.id.in_ll);
        mLight_dots = mRootView.findViewById(R.id.iv_light_dots);
        mViewPager = mRootView.findViewById(R.id.trend_bra_vp);
    }


    @Override
    protected void setListener() {
        super.setListener();
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                setLightPoint(position,positionOffset);
            }

            @Override
            public void onPageSelected(int position) {
                pageSelectedPosition=position;

                CommonEventBus eventBus=new CommonEventBus(TAG, TrendActivity.TAG,pageSelectedPosition,"pageSelectedPosition");

                EventBus.getDefault().post(eventBus);


                String title="";
                if (pageSelectedPosition==0){
                    title=getString_(R.string.dlqsh);
                }else if (pageSelectedPosition==1){
                    title=getString_(R.string.xlqsh);
                }else if (pageSelectedPosition==2){
                    title=getString_(R.string.shmqsh);
                }
                if (top_title_tv!=null) top_title_tv.setText(title);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private int pageSelectedPosition;

    public int getPageSelectedPosition() {//写这个方法能够知道这个界面滑动到那一个fragment
        return pageSelectedPosition;
    }



    @Override
    protected void initData(Bundle arguments) {
        super.initData(arguments);
        List<Fragment> listFragment = new ArrayList<>();
        listFragment.add(mTrend_Bra_duanlian_Fragment = new Trend_Bra_duanlian_Fragment());
        listFragment.add(mTrend_Bra_xinlv_Fragment = new Trend_Bra_xinlv_Fragment());
        listFragment.add(mTrend_Bra_shuimian_Fragment = new Trend_Bra_shuimian_Fragment());
        mBraFragmentAdapter = new BraFragmentAdapter(getChildFragmentManager(), listFragment);

        mViewPager.setAdapter(mBraFragmentAdapter);
        mViewPager.setOffscreenPageLimit(3);
        pageSelectedPosition = 0;
    }


    //measureType 8锻炼 9心率 10睡眠
    public void setReportData(int measureType, BaseResponse baseResponse) {
        if (measureType==8){
            if (mTrend_Bra_duanlian_Fragment!=null)mTrend_Bra_duanlian_Fragment.setDisData((BRA_DuanLian_R)baseResponse);
        }else if (measureType==9){
            if (mTrend_Bra_xinlv_Fragment!=null)mTrend_Bra_xinlv_Fragment.setDisData((BRA_XL_R)baseResponse);
        }else if (measureType==10){
            if (mTrend_Bra_shuimian_Fragment!=null)mTrend_Bra_shuimian_Fragment.setDisData((BRA_SHM_R)baseResponse);
        }


    }
}
